/**
 * @FileName: ReceiveTopic.java
 * @Package: com.rabbitmq
 * @author lig134
 * @created 2017/3/9 14:43
 * <p>
 * Copyright 2015 ziroom
 */
package com.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * <p></p>
 *
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lig134
 * @since 1.0
 * @version 1.0
 */
public class ReceiveTopic {

    public static  void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory connFac = new ConnectionFactory();
        connFac.setHost("master");
        connFac.setUsername("ligen");
        connFac.setPassword("ligen");
        //创建一个连接
        Connection conn = connFac.newConnection();

        //创建一个渠道
        Channel channel = conn.createChannel();
        String exchangeName="exchange03";
        channel.exchangeDeclare(exchangeName, "topic");
        String queueName=channel.queueDeclare().getQueue();
        //第三个参数就是type,这里表示只接受type01类型的消息 *代表一个 #代表一个或多个
        channel.queueBind(queueName,exchangeName,"*.type01");

        QueueingConsumer consumer=new QueueingConsumer(channel);
        channel.basicConsume(queueName,true,consumer);

        while(true){

            //获取消息，如果没有消息，这一步将会一直阻塞
            QueueingConsumer.Delivery delivery = consumer.nextDelivery() ;

            String msg = new String(delivery.getBody()) ;

            System.out.println("received message[" + msg + "] from " + exchangeName);
        }
    }
}